package main

import _root_.model.Note
import com.trolltech.qt.core.Qt.DockWidgetArea
import com.trolltech.qt.gui._
import com.trolltech.qt.core.Qt
import clark.main.AboutThis
import java.util.Date

/**
 * Created by IntelliJ IDEA.
 * Date: 11-2-27
 * Time: 下午4:45
 * @author Cui Gang
 * email:<a href="javascript:mailto">clarkhillm@gmail.com</a>
 */

class MainWindow extends QMainWindow {
    setWindowIcon(new QIcon("classpath:clark/icon/main.png"))
    setWindowTitle("翠鸟笔记")

    override def closeEvent(arg: QCloseEvent) = {
        println("closing...")
        arg.accept
    }

    var editorFont = font

    val self = this

    def emptyNote = new Note(-1, "", new Date, "", new QTextDocument)

    var currentNote = emptyNote

    /*    val testAction = new Action(this, "测试") {
        action.setStatusTip("插入图片测试")

        def doAction {
            val fileName = QFileDialog.getOpenFileName(self, "Open Image", "C:/", new QFileDialog.Filter("Image Files (*.png *.jpg *.bmp)"));
            println(fileName)
            if (fileName.length > 0) {
                editor.insertHtml("<img src=\"" + fileName + "\"\\>")
            }
        }
    }.getAction*/

    val fontAction = new Action(this, "字体") {
        def doAction {
            val f = QFontDialog.getFont(editor.font).font
            editor.setFontPointSize(f.pointSizeF)
            editor.setFontFamily(f.family)
        }
    }.getAction

    val newDialog = new QDialog {
        setWindowTitle("新建")
        val title = new QLineEdit {
            setText("无题")
        }
        val titleLabel = new QLabel("标题")
        val ok = new QPushButton("确定")
        ok.clicked.connect(this, "doOk()")

        def doOk {
            left.newNote(title.text)
            this.done(0)
        }

        val box = new QVBoxLayout {
            addLayout(new QHBoxLayout {
                addWidget(titleLabel)
                addWidget(title)
            })
            addWidget(new QDialogButtonBox {
                addButton(ok, QDialogButtonBox.ButtonRole.ActionRole)
            })

        }
        setLayout(box)
    }

    val newFile = new Action(this, "新建") {
        def doAction {
            newDialog.exec
        }
    }.getAction

    val aboutQt = new Action(this, "关于QT") {
        def doAction {
            QApplication.aboutQt
        }
    }.getAction

    val aboutQJ = new Action(this, "关于QtJambi") {
        def doAction {
            QApplication.aboutQtJambi
        }
    }.getAction

    val aboutMe = new Action(this, "关于") {
        def doAction {
            AboutThis.show
        }
    }.getAction

    val fileMenu = new QMenu("文件") {
        addAction(newFile)
    }

    val aboutMenu = new QMenu("关于") {
        addAction(aboutQt)
        addAction(aboutQJ)
        addAction(aboutMe)
    }

    menuBar.addMenu(fileMenu)
    menuBar.addMenu(aboutMenu)

    val editor = new QTextEdit() {
        var counter = 0
        setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        setAcceptRichText(true)

        override def focusOutEvent(e: QFocusEvent) = {
            self.saveNote(document)
        }

        textChanged.connect(this, "save()")

        def save {
            counter = counter + 1
            if (counter > 50) {
                //println(document.toHtml)
                self.saveNote(document)
                counter = 0
            }
        }
    }
    setCentralWidget(editor)

    def saveNote(text: QTextDocument) {
        currentNote = new Note(currentNote.id, currentNote.title, currentNote.date, currentNote.catalog, text)
        left.saveNote
    }

    val left = new LeftPanel(this)
    addDockWidget(DockWidgetArea.LeftDockWidgetArea, left)

    //状态栏
    val status = new QStatusBar(this)
    status.adjustSize
    setStatusBar(status)

    /*    val doSave = new Action(this, "保存") {
        action.setShortcut(QKeySequence.StandardKey.Save)

        def doAction {
            saveNote(editor.document)
        }
    }.getAction*/


    val doDelete = new Action(this, "删除") {
        def doAction {
            val yes = QMessageBox.StandardButton.Yes
            val msg = QMessageBox.question(self, "确定", "确定删除？",
                yes,
                QMessageBox.StandardButton.No)
            //println(yes.value+"-"+msg)
            if (msg == yes.value) {
                left.deleteNote
            }
        }
    }.getAction

    val toolBar = new QToolBar(this) {
        setWindowTitle("标准工具栏")
        addAction(newFile)
        //addAction(doSave)
        addAction(doDelete)
        //addAction(testAction)
        addAction(fontAction)
        addAction(left.toggleViewAction)
    }
    addToolBar(toolBar)

    resize(800, 600)
}